package com.park.service.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.park.dao.SysMenuMapper;
import com.park.model.Meta;
import com.park.model.Router;
import com.park.model.SysMenu;
import com.park.service.SysMenuService;

@Service
public class SysMenuServiceImpl implements SysMenuService {

	@Autowired
	private SysMenuMapper sysMenuMapper;
	@Override
	public List<Router> findTree(String userName) {
		// TODO Auto-generated method stub
		List<Router> routers = sysMenuMapper.getRouter(userName);
		Map<String,Object> data = new HashMap();
        try {
            //根节点
            List<Router> rootMenu = new ArrayList<Router>();
            for (Router nav : routers) {
                if(nav.getParent_id()==0){//父节点是0的，为根节点。
                    nav.setMeta(new Meta(nav.getTitle(),nav.getIcon()));
                    rootMenu.add(nav);
                }
            }
            /* 根据Menu类的order排序 */
            Collections.sort(rootMenu, order());
            //为根菜单设置子菜单，getClild是递归调用的
            for (Router nav : rootMenu) {
                /* 获取根节点下的所有子节点 使用getChild方法*/
                List<Router> childList = getChild(nav.getId(), routers);
                nav.setChildren(childList);//给根节点设置子节点
            }
            /**
             * 输出构建好的菜单数据。
             *
             */
            return rootMenu;
        } catch (Exception e) {
            return new ArrayList();
        }
	}
	 /*
     * 排序,根据order排序
     */
    public Comparator<Router> order(){
        Comparator<Router> comparator = new Comparator<Router>() {

            @Override
            public int compare(Router o1, Router o2) {
                if(o1.getOrder() != o2.getOrder()){
                    return o1.getOrder()  - o2.getOrder();
                }
                return 0;
            }
        };
        return comparator;
    }
    
    /**
     * 获取子节点
     * @param id 父节点id
     * @param allMenu 所有菜单列表
     * @return 每个根节点下，所有子菜单列表
     */
    public List<Router> getChild(Integer id,List<Router> allMenu){
        //子菜单
        List<Router> childList = new ArrayList<Router>();
        for (Router nav : allMenu) {
            // 遍历所有节点，将所有菜单的父id与传过来的根节点的id比较
            //相等说明：为该根节点的子节点。
            if(nav.getParent_id() == id){
                nav.setMeta(new Meta(nav.getTitle(),nav.getIcon()));
                childList.add(nav);
            }
        }
        //递归
        for (Router nav : childList) {
            nav.setChildren(getChild(nav.getId(), allMenu));
        }
        Collections.sort(childList,order());//排序
        //如果节点下没有子节点，返回一个空List（递归退出）
        if(childList.size() == 0){
            return new ArrayList<Router>();
        }
        return childList;
    }
	@Override
	public int insert(SysMenu sysMenu) {
		return sysMenuMapper.insert(sysMenu);
	}
	@Override
	public int update(SysMenu sysMenu) {
		// TODO Auto-generated method stub
		return sysMenuMapper.updateByPrimaryKey(sysMenu);
	}
	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		return sysMenuMapper.deleteByPrimaryKey(id);
	}
	@Override
	public List<SysMenu> getByRoleId(Long id) {
		// TODO Auto-generated method stub
		return sysMenuMapper.getByRoleId(id);
	}
	@Override
	public List<SysMenu> getAllSysMenu() {
		// TODO Auto-generated method stub
		return sysMenuMapper.getAllSysMenu();
	}

}
